package com.huawei.updatesdk.service.f;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnKeyListener;
import android.content.DialogInterface.OnShowListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import com.huawei.updatesdk.service.otaupdate.f;

public class a {
    private b a;
    private Context b;
    private String c;
    private CharSequence d;
    private AlertDialog e;
    private Builder f;
    private OnShowListener g;
    private OnDismissListener h;

    public enum a {
        CONFIRM,
        CANCEL
    }

    public interface b {
        void a();
    }

    protected a(Context context, String str, CharSequence charSequence) {
        this.b = context;
        this.c = str;
        this.d = charSequence;
        this.f = new Builder(context);
        this.f.setTitle(this.c);
        this.f.setPositiveButton(f.b(context, "upsdk_third_app_dl_sure_cancel_download"), null);
        this.f.setNegativeButton(f.b(context, "upsdk_cancel"), null);
        this.f.setMessage(this.d);
    }

    public static a a(Context context, String str, CharSequence charSequence) {
        return ((context instanceof Activity) && ((Activity) context).isFinishing()) ? new c(context, str, charSequence) : new a(context, str, charSequence);
    }

    public void a() {
        if (this.f != null) {
            this.f.setNegativeButton(null, null);
        }
    }

    public void a(int i, int i2) {
        if (com.huawei.updatesdk.service.e.b.a().b() >= 11) {
            Button button = null;
            if (this.e != null) {
                button = this.e.getButton(-1);
            }
            if (button != null) {
                button.setBackgroundResource(i);
                button.setTextColor(this.b.getResources().getColor(i2));
            }
        }
    }

    public void a(OnDismissListener onDismissListener) {
        this.h = onDismissListener;
    }

    public void a(OnKeyListener onKeyListener) {
        if (this.e != null) {
            this.e.setOnKeyListener(onKeyListener);
        }
    }

    public void a(OnShowListener onShowListener) {
        this.g = onShowListener;
    }

    public void a(View view) {
        if (this.f != null) {
            int b = com.huawei.updatesdk.service.e.b.a().b();
            if (b < 11 || b >= 17) {
                ImageView imageView = (ImageView) view.findViewById(f.a(view.getContext(), "divider"));
                if (imageView != null) {
                    imageView.setVisibility(8);
                }
            }
            this.f.setMessage(null);
            this.f.setView(view);
        }
    }

    public void a(a aVar, String str) {
        Button button = null;
        if (this.e != null) {
            if (aVar == a.CONFIRM) {
                button = this.e.getButton(-1);
            } else if (aVar == a.CANCEL) {
                button = this.e.getButton(-2);
            }
            if (button != null) {
                button.setText(str);
                button.setAllCaps(true);
            }
        }
    }

    public void a(b bVar) {
        if (this.b == null || ((Activity) this.b).isFinishing()) {
            if (bVar != null) {
                bVar.a();
            }
        } else if (b()) {
            com.huawei.updatesdk.a.a.b.a.a.a.d("BaseAlertDialog", "show dlg error, mContext = " + this.b + ", mContext.isFinishing is " + (this.b == null ? "mContext == null" : Boolean.valueOf(((Activity) this.b).isFinishing())));
        } else {
            try {
                this.e = this.f.create();
                this.e.setCanceledOnTouchOutside(false);
                this.e.setOnShowListener(new OnShowListener(this) {
                    final /* synthetic */ a a;

                    {
                        this.a = r1;
                    }

                    public void onShow(DialogInterface dialogInterface) {
                        ((AlertDialog) dialogInterface).getButton(-1).setOnClickListener(new OnClickListener(this) {
                            final /* synthetic */ AnonymousClass1 a;

                            {
                                this.a = r1;
                            }

                            public void onClick(View view) {
                                if (this.a.a.a != null) {
                                    this.a.a.a.a();
                                }
                            }
                        });
                        Button button = ((AlertDialog) dialogInterface).getButton(-2);
                        if (button != null) {
                            button.setOnClickListener(new OnClickListener(this) {
                                final /* synthetic */ AnonymousClass1 a;

                                {
                                    this.a = r1;
                                }

                                public void onClick(View view) {
                                    if (this.a.a.a != null) {
                                        this.a.a.a.b();
                                    }
                                }
                            });
                        }
                        if (this.a.g != null) {
                            this.a.g.onShow(dialogInterface);
                        }
                    }
                });
                this.e.setOnDismissListener(new OnDismissListener(this) {
                    final /* synthetic */ a a;

                    {
                        this.a = r1;
                    }

                    public void onDismiss(DialogInterface dialogInterface) {
                        if (this.a.h != null) {
                            this.a.h.onDismiss(dialogInterface);
                        }
                    }
                });
                this.e.show();
            } catch (Throwable e) {
                if (bVar != null) {
                    bVar.a();
                }
                com.huawei.updatesdk.a.a.b.a.a.a.a("BaseAlertDialog", "show dlg error, e: ", e);
            }
        }
    }

    public void a(b bVar) {
        this.a = bVar;
    }

    public void a(boolean z) {
        if (this.e != null) {
            this.e.setCancelable(z);
        }
    }

    public boolean b() {
        return this.e != null && this.e.isShowing();
    }

    public void c() {
        try {
            if (this.e != null) {
                this.e.dismiss();
                this.e = null;
            }
        } catch (IllegalArgumentException e) {
            com.huawei.updatesdk.a.a.b.a.a.a.a("BaseAlertDialog", "dialog dismiss IllegalArgumentException");
        }
    }
}
